package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.ApArticleControllerApi;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApAuthorService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController implements ApArticleControllerApi {
    @Autowired
    private ApArticleService articleService;

    @Autowired
    private ApAuthorService apAuthorService;
    @PostMapping("save")
    @Override
    public ApArticle saveArticle(@RequestBody ApArticle apArticle) {
        articleService.save(apArticle);
        return apArticle;
    }
    @GetMapping("{id}")
    @Override
    public Map findArticle(@PathVariable Long id) {
        return articleService.findArticle(id);
    }


    @GetMapping("/list/{AuthorId}")
    @Override
    public List<ApArticle> findArticleByAuthorId(@PathVariable("AuthorId") Long id) {
        ApAuthor author = apAuthorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, id));
        List<ApArticle> apArticles = articleService.list(Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getAuthorId, author.getId()));
        return apArticles;
    }





}